package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Ra;
import com.example.entity.PsLogs;
import com.example.entity.PsRole;
import com.example.entity.PsUser;
import com.example.mapper.PsRoleMapper;
import com.example.mapper.PsUserMapper;
import com.example.service.PsLogsService;
import com.example.service.impl.PsRoleServiceImpl;
import com.example.service.impl.PsUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author syj
 * @since 2024-04-27
 */
@CrossOrigin(origins="*")
@RestController
@RequestMapping("/ps-user")
public class PsUserController {
    @Autowired
    private PsUserServiceImpl psUserService;
    @Autowired
    private PsUserMapper psUserMapper;
    @Autowired
    private PsRoleServiceImpl psRoleService;
    @Autowired
    private PsRoleMapper psRoleMapper;
    @Autowired
    private PsLogsService psLogsService;

    /**
     * 分页查询用户数据
     * @param nowPage
     * @param psUser
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectPage")
    public Ra selectPage( Integer nowPage, PsUser psUser) {
        if(nowPage==null) {
            nowPage = 1;
        }
        IPage<PsUser> page = new Page<>(nowPage, 3);
        QueryWrapper<PsUser> psUserQueryWrapper = new QueryWrapper<PsUser>();
        List<PsUser> psUsers=psUserMapper.selectList(psUserQueryWrapper);
        if(psUser.getUsername()!=null && psUser.getUsername()!="") {
            psUserQueryWrapper.like("userName", psUser.getUsername());
        }
        if(psUser.getUserstatus()!=null ) {
            psUserQueryWrapper.eq("userStatus",psUser.getUserstatus());
        }
        IPage<PsUser> psUserIPage = psUserMapper.selectPage(page, psUserQueryWrapper);
        List<PsUser> records = psUserIPage.getRecords();
        records.forEach( psUser1 -> {
                    psUser1.setRolename(psRoleMapper.selectById(psUser1.getRoleid()).getRolename());
                }
        );
        long count = psUserService.count();
        return new Ra().AddDatamap("paging",records).AddDatamap("count",count);
    }

    /**
     * 添加用户
     * @param psUser
     * @return
     */
    @RequestMapping("/add")
    public  boolean AddPsUser(@RequestBody PsUser psUser, PsLogs psLogs){
        System.out.println(psUser.toString());
        psUser.setCreationtime(LocalDateTime.now());
        psUser.setEmail("2081944713@qq.com");
        psUser.setCreatedby("admin");
        psUser.setPhonetel("18175510836");
        boolean count=psUserService.save(psUser);
        if(count==true){
            System.out.println(psLogs.toString());
            psLogs.setUsercode("admin");
            psLogs.setUsername("管理员");
            psLogs.setOperateinfo("添加了用户任务");
            psLogs.setOperatedatetime(LocalDateTime.now());
            boolean count1=psLogsService.save(psLogs);
        }
        return count;
    }

    /**
     * 修改用户
     * @param psUser
     * @return
     */
    @RequestMapping("/update")
    public  boolean UpdatePsUser(@RequestBody PsUser psUser,PsLogs psLogs){
        boolean count=psUserService.updateById(psUser);
        if(count==true){
            System.out.println(psLogs.toString());
            psLogs.setUsercode("admin");
            psLogs.setUsername("管理员");
            psLogs.setOperateinfo("编辑了角色任务");
            psLogs.setOperatedatetime(LocalDateTime.now());
            boolean count1=psLogsService.save(psLogs);
        }
        return   count;
    }

    /**
     * 冻结-解冻
     * @param id
     * @param userstatus
     * @return
     */
    @RequestMapping("/UpdateUserStatus")
    public  boolean UpdateUserStatus(long id,int userstatus){
        PsUser psUser = new PsUser();
        psUser.setId(id);
        psUser.setUserstatus(userstatus);
        if (psUser.getUserstatus()==1){
            psUser.setUserstatus(0);
        }else{
            psUser.setUserstatus(1);
        }
        boolean ok=psUserService.updateById(psUser);
        return   ok;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public boolean deletePsUser( @PathVariable("id")int id,PsLogs psLogs){
        boolean count=psUserService.removeById(id);
        if(count==true){
            System.out.println(psLogs.toString());
            psLogs.setUsercode("admin");
            psLogs.setUsername("管理员");
            psLogs.setOperateinfo("删除了角色任务");
            psLogs.setOperatedatetime(LocalDateTime.now());
            boolean count1=psLogsService.save(psLogs);
        }
        return   count;
    }
}
